﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using System.Threading;
using Dala.Utilities;

namespace Dala.Utilities
{
    class GoogleMapAddressToLatLongConverter
    {
        
    }

    public interface ICoordinate
    {
        decimal Latitude { get; set; }
        decimal Longitude { get; set; }
    }

    public struct Coordinate : ICoordinate
    {
        private decimal _latitude;
        private decimal _longitude;

        public Coordinate(decimal latitude, decimal longitude)
        {
            _latitude = latitude;
            _longitude = longitude;
        }

        #region ISpatialCoordinate Members

        public decimal Latitude
        {
            get
            {
                return _latitude;
            }
            set
            {
                this._latitude = value;
            }
        }

        public decimal Longitude
        {
            get
            {
                return _longitude;
            }
            set
            {
                this._longitude = value;
            }
        }

        #endregion
    }

    public class Geocode
    {
        private const string _googleUri = "http://maps.google.com/maps/geo?q=";
        private const string _googleKey = SiteSettings._googleMapApiKey;
        private const string _outputType = SiteSettings._outputType;

        private static Uri GetGeocodeUri(string address)
        {
            address = HttpUtility.UrlEncode(address);
            return new Uri(String.Format("{0}{1}&output={2}&key={3}", _googleUri, address, _outputType, _googleKey));
        }

        public static Coordinate GetCoordinates(string address)
        {
            WebClient client = new WebClient();
            Uri uri = GetGeocodeUri(address);

            /* The first number is the status code, 
            * the second is the accuracy, 
            * the third is the latitude, 
            * the fourth one is the longitude.
            */
            string[] geocodeInfo = client.DownloadString(uri).Split(',');

            return new Coordinate(Convert.ToDecimal(geocodeInfo[2]), Convert.ToDecimal(geocodeInfo[3]));
        }

        //Demo of how to convert address to Lat Long
        public class Address
        {
            public string AddressLine { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string ZipCode { get; set; }
            public decimal Latitude { get; set; }
            public decimal Longitude { get; set; }
        }

        public static void ConvertAllDummyAddressToLatLone()
        {
            List<Address> _addresses = null;

            foreach (var item in _addresses)
            {
                string addressInString = item.AddressLine + ", " + item.City + ", " + item.State + ", " + item.ZipCode;

                Coordinate _co = new Coordinate();
                _co = GetCoordinates(addressInString);

                item.Latitude = _co.Latitude;
                item.Longitude = _co.Longitude;

                //Use 2-3 sec break or else Google map will Response Over Query Limit Exception
                Thread.Sleep(3000);
            }
        }
    }
}
